import Vue from 'vue'
import App from './App.vue'
//引入elementui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import store from "./store/index"
// 引入video.js
import Videojs from 'video.js'
import 'video.js/dist/video-js.css'
Vue.prototype.$video = Videojs
//引入地址库
import Distpicker from 'v-distpicker';
Vue.component('v-distpicker', Distpicker);
//配置路由
import router from './router/index'
//axios
import axios from 'axios';
Vue.prototype.axios = axios
axios.defaults.baseURL = 'http://localhost:8002/api'
axios.defaults.timeout = 5000

axios.interceptors.request.use(
  config => {
    if (localStorage.getItem('token')) {
      config.headers.Authorization = localStorage.getItem('token')
    }
    return config
  },
  err => {
    return { err }
  }
)
axios.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 403:
          ElementUI.Message({
            message: '您的身份已过期，请重新登录',
            type: 'error',
            showClose: true,
          })
          store.commit('delToken')
          router.replace({
            path: 'login',
            query: { redirect: router.currentRoute.fullPath }
          });
      }
    }
    //网络超时异常处理
    if (error.code === 'ECONNABORTED' || error.message === "Network Error" || error.message.includes("timeout")) {
      ElementUI.Message({
        message: '请求超时，请稍后重试',
        type: "error",
        duration: 3 * 1000,
      })
    }
    return Promise.resolve(error.response);
  }
)

//清楚浏览器自带的样式
import './assets/css/resrt.css'
Vue.config.productionTip = false
Vue.use(ElementUI)

//注册头像裁剪插件
import VueCropper from 'vue-cropper'
Vue.use(VueCropper)
new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app')
